package com.endbd.warehouse.service.impl;

import com.endbd.feignapi.model.system.City;
import com.endbd.feignapi.model.system.Province;
import com.endbd.feignapi.model.system.User;
import com.endbd.feignapi.model.warehouse.CkWarehouse;
import com.endbd.feignapi.model.warehouse.KcWarehouse;
import com.endbd.feignapi.model.warehouse.RkWarehouse;
import com.endbd.feignapi.model.warehouse.Warehouse;
import com.endbd.feignapi.util.PageModel;
import com.endbd.warehouse.dao.IManageDao;
import com.endbd.warehouse.model.*;
import com.endbd.warehouse.service.IManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageService implements IManageService {
    @Autowired
    private IManageDao manageDao;
    public IManageDao getManageDao() {
        return manageDao;
    }
    public void setManageDao(IManageDao manageDao) {
        this.manageDao = manageDao;
    }

    @Override
    public PageModel<Warehouse> queryWarehouses(WarehouseCondition wc, PageModel<Warehouse> pageModel) {
        int current = pageModel.getPageNum();
        current = current<=0?1:current;
        int pageSize = pageModel.getPageSize();
        pageSize = pageSize<=0?5:pageSize;
        PageHelper.startPage(current,pageSize);
        List<Warehouse> list = manageDao.queryWarehouses(wc);
        PageInfo<Warehouse> info = new PageInfo<Warehouse>(list);

        pageModel.setPageNum(current);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(new ArrayList<Warehouse>(list));
        return pageModel;
    }

    @Override
    public Warehouse queryWarehouse(WarehouseCondition wc) {
        return manageDao.queryWarehouse(wc);
    }

    @Override
    public List<Province> queryProvinces(ProvinceCondition pc) {
        return manageDao.queryProvinces(pc);
    }

    @Override
    public List<City> queryCitys(CityCondition cc) {
        return manageDao.queryCitys(cc);
    }

    @Override
    @Transactional
    public int changeState(String id) {
        return manageDao.changeState(id);
    }

    @Override
    @Transactional
    public int recoverState(String id) {
        return manageDao.recoverState(id);
    }

    @Override
    public int updateWarehouse(Warehouse wh) {
        return manageDao.updateWarehouse(wh);
    }

    @Override
    public List<User> queryUsers(UserCondition uc) {
        return manageDao.queryUsers(uc);
    }

    @Override
    public int insertone(Warehouse w) {
        w.setWMoney(0.0);
        return manageDao.insertone(w);
    }

    @Override
    public PageModel<Warehouse> querys(WarehouseCondition wc, PageModel<Warehouse> pageModel) {
        int current = pageModel.getPageNum();
        current = current<=0?1:current;
        int pageSize = pageModel.getPageSize();
        pageSize = pageSize<=0?5:pageSize;
        PageHelper.startPage(current,pageSize);
        List<Warehouse> list = manageDao.querys(wc);
        PageInfo<Warehouse> info = new PageInfo<Warehouse>(list);

        pageModel.setPageNum(current);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(new ArrayList<Warehouse>(list));
        return pageModel;
    }

    @Override
    public PageModel<RkWarehouse> queryRK(RKCondition rkc, PageModel<RkWarehouse> pageModel) {
        int current = pageModel.getPageNum();
        current = current<=0?1:current;
        int pageSize = pageModel.getPageSize();
        pageSize = pageSize<=0?5:pageSize;
        PageHelper.startPage(current,pageSize);
        List<RkWarehouse> list = manageDao.queryRK(rkc);
        PageInfo<RkWarehouse> info = new PageInfo<RkWarehouse>(list);

        pageModel.setPageNum(current);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(new ArrayList<RkWarehouse>(list));
        return pageModel;
    }

    @Override
    public int updateRKState(String id, String state) {
        return manageDao.updateRKState(id,state);
    }

    @Override
    public PageModel<CkWarehouse> queryCK(CKCondition ckc, PageModel<CkWarehouse> pageModel) {
        int current = pageModel.getPageNum();
        current = current<=0?1:current;
        int pageSize = pageModel.getPageSize();
        pageSize = pageSize<=0?5:pageSize;
        PageHelper.startPage(current,pageSize);
        List<CkWarehouse> list = manageDao.queryCK(ckc);
        PageInfo<CkWarehouse> info = new PageInfo<CkWarehouse>(list);

        pageModel.setPageNum(current);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(new ArrayList<CkWarehouse>(list));
        return pageModel;
    }

    @Override
    public int updateCKState(String id, String state) {
        return manageDao.updateCKState(id,state);
    }

    @Override
    @Transactional
    public int addCK(CkWarehouse ckw) {
        ckw.setState("1");
        return manageDao.addCK(ckw);
    }

    @Override
    @Transactional
    public int addRK(RkWarehouse rkw) {
        rkw.setState("1");
        return manageDao.addRK(rkw);
    }

    @Override
    public PageModel<KcWarehouse> querymanages(KCCondition kcc, PageModel<KcWarehouse> pageModel) {
        int current = pageModel.getPageNum();
        current = current<=0?1:current;
        int pageSize = pageModel.getPageSize();
        pageSize = pageSize<=0?5:pageSize;
        PageHelper.startPage(current,pageSize);
        List<KcWarehouse> list = manageDao.querymanages(kcc);
        PageInfo<KcWarehouse> info = new PageInfo<>(list);


        pageModel.setPageNum(current);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(new ArrayList<KcWarehouse>(list));
        return pageModel;
    }

    @Override
    public List<Warehouse> queryAll(WarehouseCondition wc) {
        return manageDao.queryWarehouses(wc);
    }

    @Override
    @Transactional
    public int changRk2(IDCondition idc) {
        System.out.println("**********************");
        System.out.println(idc);
        int kc_num = manageDao.selectRk2(idc);
        int dd_num = Integer.parseInt(idc.getTotal());
        int num = kc_num+dd_num;
        idc.setTotal(num+"");
        int count = manageDao.changRk2(idc);
        return count;
    }


}
